package net.dgg.dggcloud.service.jrcrm.service.mq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Envelope;
import net.dgg.dggcloud.core.cache.CacheManager;
import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.entity.org.OrganizationEntity;
import net.dgg.dggcloud.service.jrcrm.constant.CommonConstant;
import net.dgg.dggcloud.service.service.organization.OrganizationService;
import net.dgg.framework.tac.mq.rabbitmq.queue.normal.DggAbstractNormalConsumer;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.poi.util.StringUtil;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * IBOSS用户消息处理
 *
 * @Title: OrgConsumer
 * @Description:
 * @Auther: 汤宏
 * @Date: 2018/06/19 9:15
 */
public class OrgConsumer extends DggAbstractNormalConsumer<Map> {
    @Resource
    private OrganizationService organizationService;

    public OrgConsumer() {
        this.queueName = "net.dgg.dggcloud.service.jrcrm.service.mq.OrganizationConsumer";
        this.hasDeadQueue = false;
    }
    public void setExchangeName(String exchangeName) {
        this.exchangeName = exchangeName;
    }

    @Override
    protected void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, Map data) throws IOException {
    //    logger.info("IBOSS部门消息处理：{}", data);
        try {
            if (null != data) {
                Map orgMap = (Map) data.get("organizationDTO");
                OrganizationEntity org = null;
                if(orgMap.get("id")!=null){
                    org = organizationService.find4Id(NumberUtils.toLong(orgMap.get("id")+""));
                    if(org==null){
                        org = new OrganizationEntity();
                    }
                }else {
                    org = new OrganizationEntity();
                }
                org.setId(Long.parseLong(orgMap.get("id").toString()));

                org.setCode(null != orgMap.get("code") ? orgMap.get("code").toString() : "");
                org.setName(orgMap.get("name").toString());
              // org.setLeaderId(null != orgMap.get("leaderId") ? Long.parseLong(orgMap.get("leaderId").toString()) : null);
              //  org.setLeaderName(null != orgMap.get("leaderName") ? orgMap.get("leaderName").toString() : "");
                org.setEnabled("1".equals(orgMap.get("enabled").toString())?true:false);
                org.setpId(Long.parseLong(null != orgMap.get("pId") ? orgMap.get("pId").toString() : "0"));
                org.setSort(null != orgMap.get("sort") ? Integer.parseInt(orgMap.get("sort").toString()) : 0);
                org.setDescription(null != orgMap.get("description") ? orgMap.get("description").toString() : "");
                org.setType(null != orgMap.get("type") ? orgMap.get("type").toString():"");
                organizationService.saveMqOrg(org);
                CacheManager.getOperator().put(CommonConstant.ORGANIZATION_KEY + org.getId().toString(), org.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
  //          logger.info("部门消息处理异常：{}", data);
        }
    }
}
